package com.mf.morefun.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.ReadDetailTypeBean;
import com.mf.morefun.listener.ReadDetailTypeListener;

import java.util.List;

public class ReadDetailTypeAdapter extends RecyclerView.Adapter<ReadDetailTypeAdapter.ViewHolder>{
    private Context mContext;
    private List<ReadDetailTypeBean> mReadDetailTypeList;
    private int selectIndex = 0;
    private LayoutInflater inflater;
    private ReadDetailTypeListener listener;

    public ReadDetailTypeListener getListener() {
        return listener;
    }

    public void setListener(ReadDetailTypeListener listener) {
        this.listener = listener;
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public ReadDetailTypeAdapter(Context Context, List<ReadDetailTypeBean> mReadDetailTypeList) {
        this.mContext = Context;
        this.mReadDetailTypeList = mReadDetailTypeList;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_readdetailtypeitem, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.view.setTag(position);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.callBack((int)(v.getTag()));
            }
        });
        //
        if (getSelectIndex() == position){
            holder.tvType.setTextColor(0xffffffff);
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.main_default_color));
        }else {
            holder.view.setBackgroundColor(0xffffffff);
            holder.tvType.setTextColor(0xff333333);
        }
        holder.tvType.setText(mReadDetailTypeList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mReadDetailTypeList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
       TextView tvType;
       View view = null;
        public ViewHolder(View itemView) {
            super(itemView);
            tvType = itemView.findViewById(R.id.tv_type);
            view = itemView;
        }
    }
}
